package com.servertest1;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Account extends Activity implements OnClickListener
{
	private Button btn1;
	private EditText usr;
	private EditText pswrd;
	private EditText cnfrm;

	private String username;
	private String password;
	private String confirm;
	private Client client;

	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account);

		// Link button to variable
		btn1 = (Button) findViewById(R.id.btnSubmitAccount);

		// Link text fields to variables
		usr = (EditText) findViewById(R.id.newUsername);
		pswrd = (EditText) findViewById(R.id.newPassword);
		cnfrm = (EditText) findViewById(R.id.confirm);

		// Set click listener
		btn1.setOnClickListener(this);
		client = new Client();
	}

	public void onClick(View v) 
	{
		switch (v.getId())
		{
			case R.id.btnSubmitAccount:
				createAccount();
				break;
			default:
				break;
			}
	}

	public void createAccount() {
		username = usr.getText().toString();
		password = pswrd.getText().toString();
		confirm = cnfrm.getText().toString();

		// Make sure password and confirm are the same
		if (!password.equals(confirm))
		{
			Toast.makeText(this, "Passwords are not the same", Toast.LENGTH_LONG).show();
			return;
		}
		//Make sure user name and password are all letters or numbers
		if(!((usefulMethods.isAllNumberOrAlpha(username) && usefulMethods.isAllNumberOrAlpha(password)))){
			Toast.makeText(this, "Only letters and numbers allowed in usernames and passwords", Toast.LENGTH_LONG).show();
			return;
		}
		//username and password length should be between 5 and 15
		if(username.length() < 5 || username.length() > 15 || password.length() < 5 || password.length() > 15){
			Toast.makeText(this, "Username and password must be between 5 and 15 characters.", Toast.LENGTH_LONG).show();
			return;
		}


		try {
			password = SHA1.SHA1encrypt(password);
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		//Attempt to create new account
		String[] cmd = new String[3];
		cmd[0] = "newUser";
		cmd[1] = username;
		cmd[2] = password;
		
		new CreateAccountTask().execute(cmd);
	}
	private class CreateAccountTask extends AsyncTask<String, Void, String[]>{

		protected String[] doInBackground(String... cmd) {
			client.connect();
			client.sendData(cmd);
			String result[] = client.recieveData();
			client.close();
			
			return result;
		}
		
		protected void onPostExecute(String[] result){
			if(result[0].equals("added")){
				finish();
			}
			else if(result[0].equals("user_already_in_database")){
				Toast.makeText(Account.this, "The username already exists.", Toast.LENGTH_LONG).show();
			}
			else{
				Toast.makeText(Account.this, "Database returned something wierd: " + result[0], Toast.LENGTH_LONG).show();
			}
		}
	}
}